package cn.edu.ctbu.studentmanage.api;

import cn.edu.ctbu.studentmanage.core.PageUtils;
import cn.edu.ctbu.studentmanage.core.RUtil;
import cn.edu.ctbu.studentmanage.domain.Course;
import cn.edu.ctbu.studentmanage.domain.Lecture;
import cn.edu.ctbu.studentmanage.service.LectureService;
import cn.edu.ctbu.studentmanage.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;

@RestController
@RequestMapping("/api")
@Slf4j
public class LectureApiController {

    @Autowired
    private LectureService lectureService;

    /**
     * 读取教师授课信息./api/lectureindex
     * @return
     */
    @GetMapping("/lectureindex")
    public R findAll(){
        return RUtil.success(lectureService.findAll());
    }

    /**
     * 根据id查找授课信息./api/lectureget/lid
     * @param lid
     * @return
     */
    @GetMapping("/lectureget/{lid}")
    public R get(@PathVariable Integer lid) {
        Lecture lecture = lectureService.getByLid(lid);

        return RUtil.success(lecture);
    }

    /**
     * 新增授课信息./api/lectureinsert
     * @param lecture
     * @param bindingResult
     * @return
     */
    @PostMapping("/lectureinsert")
    public R insert(@Valid Lecture lecture, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String errStr = bindingResult.getFieldError().getDefaultMessage();
            log.error(errStr);
        }
        lectureService.Insert(lecture);

        log.info(lecture.toString());
        return RUtil.success(null);
    }

    /**
     * 更新授课信息./api/lectureupdate
     * @param lecture
     * @return
     */
    @PutMapping ("/lectureupdate")
    public R update(Lecture lecture){

        lectureService.update(lecture);

        log.info(lecture.toString());
        return RUtil.success(null);
    }

    /**
     * 根据id删除某一授课信息./api/lecturedelete/lid
     * @param lid
     * @return
     */
    @DeleteMapping ("/lecturedelete/{lid}")
    public R delete(@PathVariable Integer lid){

        lectureService.delete(lid);

//        log.info(Course.toString());
        return RUtil.success(null);
    }

    /**
     *
     * 分页读取全部内容./api/getbypagelecture
     * @return
     */
    @GetMapping("/getbypagelecture")
    public R findByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                @RequestParam(value = "size",defaultValue = "10")Integer size,
                                @RequestParam(value = "tname",defaultValue = "")String tname,
                                @RequestParam(value = "cname",defaultValue = "")String cname
    ){

        Page<Lecture> lecturePage ;
        if(StringUtils.isEmpty(cname)&&StringUtils.isEmpty(tname)
        ){
            Pageable pageable = PageRequest.of(page,size, Sort.by("lid"));
            lecturePage =lectureService.findAll(pageable);
        }else {
            Lecture lecture = new Lecture();
            lecture.setCname(cname);
            lecture.setTname(tname);
            Pageable pageable = PageRequest.of(page,size,Sort.by("lid"));
            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("tname",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("cname",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Lecture> example = Example.of(lecture,matcher);
//        Example<Course> example = Example.of(Course);
            lecturePage = lectureService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(lecturePage.getContent(),lecturePage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}